<template>
	<view class="container">
		<view v-for="(item, index) in layoutData" :key="index">
			<!-- @click="navToDetailPage(item)" -->

			<!-- 小程序头部兼容 -->
			<!-- #ifdef MP -->
			<view v-if="item.type == 'auxiliary_search'" class="mp-search-box">
				<input class="ser-input" type="text" value="输入关键字搜索" disabled />
			</view>
			<!-- #endif -->

			<!-- 头部轮播 -->
			<view v-if="item.type == 'imgtxt_slide' && item.data.size == 'normal'" class="carousel-section">
				<!-- 标题栏和状态栏占位符 -->
				<view class="titleNview-placing"></view>
				<!-- 背景色区域 -->
				<view class="titleNview-background" :style="{backgroundColor:titleNViewBackground}"></view>
				<swiper class="carousel" circular @change="swiperChange">
					<swiper-item v-for="(imgitem, index) in item.data.list" :key="index" class="carousel-item" @click="navToDetailPage({title: '轮播广告'})">
						<image :src="imgitem[0].img" />
					</swiper-item>
				</swiper>
				<!-- 自定义swiper指示器 -->
				<view class="swiper-dots">
					<text class="num">{{swiperCurrent+1}}</text>
					<text class="sign">/</text>
					<text class="num">{{item.data.list.length}}</text>
				</view>
			</view>

			<view v-if="item.type == 'auxiliary_gap'" :style="{height:item.data.height*3+'rpx',backgroundColor:item.data.modBGColor?item.data.modBGColor:'#ffffff'}"></view>
			
			<!-- 标题 -->
			<view v-if="item.type == 'auxiliary_title'" class="auxiliary-title" :style="{height:'58rpx','border-left':(item.data.color?item.data.color:'#737373')+' 6rpx solid' }"><text class="auxiliary-title-text">{{item.data.title}}</text></view>
			
			<!-- 导航分类5列 -->
			<view v-if="item.type == 'nav_grid-five'" class="nav-grid-five">
				<view v-for="(gfitem,index) in item.data.list" :key="index" class="nav-grid-five-item">
					<image :src="gfitem.content" class="grid-five-item-img" />
					<text v-if="!gfitem.content||item.data.isShowTitle==1" class="nav-grid-five-item-title">{{gfitem.title}}</text>
				</view>
			</view>
			<!-- 导航分类4列 -->
			<view v-if="item.type == 'nav_grid-four'" class="nav-grid-four">
				<view v-for="(gfitem,index) in item.data.list" :key="index" class="nav-grid-four-item">
					<image :src="gfitem.content" class="grid-four-item-img" />
					<text v-if="!gfitem.content||item.data.isShowTitle==1" class="nav-grid-four-item-title">{{gfitem.title}}</text>
				</view>
			</view>
			<!-- 标签4列 -->
			<view v-if="item.type == 'nav_text-tag'" class="nav-text-tag">
				<view v-for="(gfitem,index) in item.data.list" :key="index" class="nav-text-tag-item">
					<text class="nav-text-tag-item-title">{{gfitem.title}}</text>
				</view>
			</view>
			<!-- 自动高度图片 -->
			<view v-if="item.type == 'imgtxt_autoheight'" class="imgtxt-autoheight">
				<image :src="item.data.img" mode="widthFix" />
			</view>

			<view v-if="item.type == 'imgtxt_l1r2b'" class="imgtxt-l1r2b">
				<view class="imgtxt-l1r2b-l1r">
					<image :src="item.data.list[0].img" mode="scaleToFill" />
				</view>
				<view class="imgtxt-l1r2b-l2b">
					<image :src="item.data.list[1].img" mode="scaleToFill" />
					<image :src="item.data.list[2].img" mode="scaleToFill" />
				</view>
			</view>

			<view v-if="item.type == 'product_col2'" class="guess-section">
				<view v-for="(gitem, index) in item.data.list[0]" :key="index" class="guess-item" @click="navToDetailPage(gitem)">
					<view class="image-wrapper">
						<image src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553187020783&di=bac9dd78b36fd984502d404d231011c0&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201609%2F26%2F20160926173213_s5adi.jpeg"
						 mode="aspectFill"></image>
					</view>
					<text class="title clamp">{{gitem.name}}</text>
					<text class="price">￥{{gitem.priceReal}}</text>
				</view>
			</view>

			<view v-if="item.type == 'imgtxt_slide' && item.data.size == 'normal'" class="carousel-section">

			</view>

			<!-- 循环结束 -->
		</view>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				layoutData: [],
				swiperCurrent: 0,
				swiperLength: 0,
				carouselList: [],
				goodsList: []
			};
		},

		onLoad() {
			this.loadData();
		},
		onShow() {
			console.log('index Show')
		},
		methods: {
			/**
			 * 请求静态数据只是为了代码不那么乱
			 * 分次请求未作整合
			 */
			async loadData() {
				console.log('开始加载数据...');
				this.$http.postJson('http://localhost:8182/homeLayout/findRecordOne', {
					id: 5
				}).then(data => {
					var [error, res] = data;
					console.log(error, res.data);
					if (res.data.code == 0) {
						this.homeLayout = res.data.data;
						let layoutData = res.data.data.saveData;
						this.layoutData = JSON.parse(layoutData);

					}
				})
				let carouselList = await this.$api.json('carouselList');
				this.titleNViewBackground = carouselList[0].background;
				this.swiperLength = carouselList.length;
				this.carouselList = carouselList;

				let goodsList = await this.$api.json('goodsList');
				this.goodsList = goodsList || [];
			},
			//轮播图切换修改背景色
			swiperChange(e) {
				const index = e.detail.current;
				this.swiperCurrent = index;
				this.titleNViewBackground = this.carouselList[index].background;
			},
			//详情页
			navToDetailPage(item) {
				//测试数据没有写id，用title代替
				let id = item.title;
				uni.navigateTo({
					url: `/pages/product/product?id=${id}`
				})
			},
		},
		// #ifndef MP
		// 标题栏input搜索框点击
		onNavigationBarSearchInputClicked: async function(e) {
			this.$api.msg('点击了搜索框');
		},
		//点击导航栏 buttons 时触发
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.$api.msg('点击了扫描');
			} else if (index === 1) {
				// #ifdef APP-PLUS
				const pages = getCurrentPages();
				const page = pages[pages.length - 1];
				const currentWebview = page.$getAppWebview();
				currentWebview.hideTitleNViewButtonRedDot({
					index
				});
				// #endif
				uni.navigateTo({
					url: '/pages/notice/notice'
				})
			}
		}
		// #endif
	}
</script>

<style lang="scss">
	.imgtxt-autoheight {
		width: 100%;
		// height: 150upx;
		background: #fff;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.imgtxt-l1r2b {
		display: flex;
		justify-content: space-between;
	}

	.imgtxt-l1r2b-l1r {
		width: 50%;
		height: 420upx;
		margin: 10upx 10upx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.imgtxt-l1r2b-l2b {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		width: 50%;
		height: 420upx;
		margin: 10upx 10upx;

		image {
			width: 100%;
			height: 200upx;
		}
	}

	/* 分类 5*/
	.nav-grid-five {
		display: flex;
		justify-content: flex-start;
		flex-wrap: wrap;
		// align-items: center;
		// align-content:center;
		padding: 0 22upx;
		background: #fff;

		.nav-grid-five-item {
			display: flex;
			width: 20%;
			flex-direction: column;
			align-items: center;
			align-content: center;
			font-size: $font-sm + 2upx;
			color: $font-color-dark;
		}

		/* 原图标颜色太深,不想改图了,所以加了透明度 */
		image {
			width: 88upx;
			height: 88upx;
			margin-bottom: 14upx;
			// border-radius: 50%;
			opacity: .7;
			// box-shadow: 4upx 4upx 20upx rgba(250, 67, 106, 0.3);
		}
	}

	/* 分类4 */
	.nav-grid-four {
		display: flex;
		justify-content: flex-start;
		flex-wrap: wrap;
		// align-items: center;
		// align-content:center;
		padding: 0 22upx;
		background: #fff;

		.nav-grid-four-item {
			display: flex;
			width: 25%;
			flex-direction: column;
			align-items: center;
			align-content: center;
			font-size: $font-sm + 2upx;
			color: $font-color-dark;
		}

		/* 原图标颜色太深,不想改图了,所以加了透明度 */
		image {
			width: 88upx;
			height: 88upx;
			margin-bottom: 14upx;
			// border-radius: 50%;
			opacity: .7;
			// box-shadow: 4upx 4upx 20upx rgba(250, 67, 106, 0.3);
		}
	}
	/* 标签4 */
	.nav-text-tag {
		display: flex;
		justify-content: flex-start;
		flex-wrap: wrap;
		padding: 0 22upx;
		background: #fff;

		.nav-text-tag-item {
			display: flex;
			width: 25%;
			flex-direction: column;
			align-items: center;
			align-content: center;
			font-size: $font-sm + 2upx;
			color: $font-color-dark;
			margin-top: 6rpx;
			margin-bottom: 6rpx;
			text{
				width: 80%;
				text-align: center;
				margin: auto 0;
				border: #dfdfdf 1rpx solid;
			}
		}
	}
	.auxiliary-title{
		background: #F8F8F8;
	
		text{
			margin-left: 8rpx;
			font-size: $font-lg + 2upx;
		}
	}
	/* #ifdef MP */
	.mp-search-box {
		position: absolute;
		left: 0;
		top: 30upx;
		z-index: 9999;
		width: 100%;
		padding: 0 80upx;

		.ser-input {
			flex: 1;
			height: 56upx;
			line-height: 56upx;
			text-align: center;
			font-size: 28upx;
			color: $font-color-base;
			border-radius: 20px;
			background: rgba(255, 255, 255, .6);
		}
	}

	page {
		.cate-section {
			position: relative;
			z-index: 5;
			border-radius: 16upx 16upx 0 0;
			margin-top: -20upx;
		}

		.carousel-section {
			padding: 0;

			.titleNview-placing {
				padding-top: 0;
				height: 0;
			}

			.carousel {
				.carousel-item {
					padding: 0;
				}
			}

			.swiper-dots {
				left: 45upx;
				bottom: 40upx;
			}
		}
	}

	/* #endif */


	page {
		background: #f5f5f5;
	}

	.m-t {
		margin-top: 16upx;
	}

	/* 头部 轮播图 */
	.carousel-section {
		position: relative;
		padding-top: 10px;

		.titleNview-placing {
			height: var(--status-bar-height);
			padding-top: 44px;
			box-sizing: content-box;
		}

		.titleNview-background {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 426upx;
			transition: .4s;
		}
	}

	.carousel {
		width: 100%;
		height: 350upx;

		.carousel-item {
			width: 100%;
			height: 100%;
			padding: 0 28upx;
			overflow: hidden;
		}

		image {
			width: 100%;
			height: 100%;
			border-radius: 10upx;
		}
	}

	.swiper-dots {
		display: flex;
		position: absolute;
		left: 60upx;
		bottom: 15upx;
		width: 72upx;
		height: 36upx;
		background-image: url();
		background-size: 100% 100%;

		.num {
			width: 36upx;
			height: 36upx;
			border-radius: 50px;
			font-size: 24upx;
			color: #fff;
			text-align: center;
			line-height: 36upx;
		}

		.sign {
			position: absolute;
			top: 0;
			left: 50%;
			line-height: 36upx;
			font-size: 12upx;
			color: #fff;
			transform: translateX(-50%);
		}
	}

	/* 猜你喜欢 */
	.guess-section {
		display: flex;
		flex-wrap: wrap;
		padding: 0 30upx;
		background: #fff;

		.guess-item {
			display: flex;
			flex-direction: column;
			width: 48%;
			padding-bottom: 40upx;

			&:nth-child(2n+1) {
				margin-right: 4%;
			}
		}

		.image-wrapper {
			width: 100%;
			height: 330upx;
			border-radius: 3px;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}

		.title {
			font-size: $font-lg;
			color: $font-color-dark;
			line-height: 80upx;
		}

		.price {
			font-size: $font-lg;
			color: $uni-color-primary;
			line-height: 1;
		}
	}
</style>
